package com.example.monitor;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Collection;

import javax.management.remote.JMXServiceURL;

import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.QueueViewMBean;
import org.apache.activemq.broker.jmx.TopicViewMBean;
import org.apache.activemq.web.RemoteJMXBrokerFacade;
import org.apache.activemq.web.config.AbstractConfiguration;
import org.apache.activemq.web.config.SystemPropertiesConfiguration;


public class ActivemqMonitor {

	/**
	 * @param args
	 */

	public static void main(String[] args) {
		
		ActivemqMonitor d=new ActivemqMonitor();
		d.ss();
	}

	public void ss(){
		RemoteJMXBrokerFacade createConnector = new RemoteJMXBrokerFacade();  
		System.setProperty("webconsole.jmx.url","service:jmx:rmi:///jndi/rmi://192.168.200.128:11099/jmxrmi");
		System.setProperty("webconsole.jmx.user", "admin");  
		System.setProperty("webconsole.jmx.password", "activemq"); 
		//System.setProperty("java.rmi.server.hostname" , "172.16.3.53" ); 
		SystemPropertiesConfiguration configuration = new SystemPropertiesConfiguration();  
		
		createConnector.setConfiguration(configuration);
		
		//createConnector.setConfiguration(new ProprtiesConfiguration("service:jmx:rmi:///jndi/rmi://172.16.3.53:11099/jmxrmi","admin","adminmq"));
		try {
			//createConnector.setBrokerName("localhost");
			
		
	
			BrokerViewMBean brokerAdmin = createConnector.getBrokerAdmin();
			
			System.out.println("==========Connection =================");  
			System.out.println("BrokerName =" + brokerAdmin.getBrokerName());  
			System.out.println("TotalMessageCount =" + brokerAdmin.getTotalMessageCount());  
			System.out.println("TotalConsumerCount =" + brokerAdmin.getTotalConsumerCount());  
			System.out.println("TotalDequeueCount =" + brokerAdmin.getTotalDequeueCount());  
			System.out.println("TotalEnqueueCount =" + brokerAdmin.getTotalEnqueueCount()); 
			System.out.println("==_=="+brokerAdmin.getStorePercentUsage());
			
			
			Collection<TopicViewMBean> topicViewList = createConnector.getTopics();  
			for (TopicViewMBean topicViewMBean : topicViewList) {  
				System.out.println("=============Topic =================");  
			    System.out.println("beanName =" + topicViewMBean.getName());  
			    System.out.println("ConsumerCount ="+ topicViewMBean.getConsumerCount());  
			    System.out.println("DequeueCount ="+ topicViewMBean.getDequeueCount());  
			    System.out.println("EnqueueCount ="+ topicViewMBean.getEnqueueCount());  
			    System.out.println("DispatchCount ="+ topicViewMBean.getDispatchCount());  
			    System.out.println("ExpiredCount ="+ topicViewMBean.getExpiredCount());  
			    System.out.println("MaxEnqueueTime ="+ topicViewMBean.getMaxEnqueueTime());  
			    System.out.println("ProducerCount ="+ topicViewMBean.getProducerCount());  
			    System.out.println("MemoryPercentUsage ="+ topicViewMBean.getMemoryPercentUsage());  
			    System.out.println("MemoryLimit =" + topicViewMBean.getMemoryLimit());  
			    
			}
			
			Collection<QueueViewMBean> queues=createConnector.getQueues();
			
			for(QueueViewMBean queueViewMBean:queues){
				System.out.println("=========queue========");
				System.out.println(queueViewMBean.getName());
				System.out.println("=="+queueViewMBean.getCursorPercentUsage());
				//brokerAdmin.removeQueue(queueViewMBean.getName());
				System.out.println("example="+queueViewMBean.getProducerCount());
				
			} 
			System.out.println("queues="+createConnector.getConnector("openwire").toString());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	class ProprtiesConfiguration extends AbstractConfiguration{
		private String url;
	    private String user;
	    private String passwd;
	    
	    public ProprtiesConfiguration(){}
	    public ProprtiesConfiguration(String url, String user, String passwd) {
			this.url = url;
			this.user = user;
			this.passwd = passwd;
		}

		public Collection<JMXServiceURL> getJmxUrls() {
			return makeJmxUrls(url);
		}

		public String getJmxPassword() {
			return passwd;
		}

		public String getJmxUser() {
			return user;
		}
		
	}

}
